using HousingInfo.DataLinQ;

using System;
using System.Linq;
using Csla;
using Csla.Data;
using Csla.Security;
using Csla.Validation;

namespace HousingInfo.Library
{ 
	[Serializable()] 
	public partial class ForumNewsBO : Csla.BusinessBase<ForumNewsBO>
	{
		#region Business Properties and Methods
		//register properties
		private static PropertyInfo<int> ForumNewIDProperty = RegisterProperty<int>(typeof(ForumNewsBO), new PropertyInfo<int>("ForumNewID"));
		private static PropertyInfo<string> UserNameProperty = RegisterProperty<string>(typeof(ForumNewsBO), new PropertyInfo<string>("UserName"));
        private static PropertyInfo<int?> SubTopicForumIDProperty = RegisterProperty<int?>(typeof(ForumNewsBO), new PropertyInfo<int?>("SubTopicForumID"));
		private static PropertyInfo<string> TitleProperty = RegisterProperty<string>(typeof(ForumNewsBO), new PropertyInfo<string>("Title"));
		private static PropertyInfo<string> ForumNewsContentProperty = RegisterProperty<string>(typeof(ForumNewsBO), new PropertyInfo<string>("ForumNewsContent"));
		private static PropertyInfo<string> UrlImageProperty = RegisterProperty<string>(typeof(ForumNewsBO), new PropertyInfo<string>("UrlImage"));
		private static PropertyInfo<DateTime?> CreatedDateProperty = RegisterProperty<DateTime?>(typeof(ForumNewsBO), new PropertyInfo<DateTime?>("CreatedDate"));
		private static PropertyInfo<DateTime?> UpdatedDateProperty = RegisterProperty<DateTime?>(typeof(ForumNewsBO), new PropertyInfo<DateTime?>("UpdatedDate"));
		private static PropertyInfo<bool?> IsViolationProperty = RegisterProperty<bool?>(typeof(ForumNewsBO), new PropertyInfo<bool?>("IsViolation"));
        private static PropertyInfo<string> UserNameReportProperty = RegisterProperty<string>(typeof(ForumNewsBO), new PropertyInfo<string>("UserNameReport"));
        private static PropertyInfo<string> NumberReportProperty = RegisterProperty<string>(typeof(ForumNewsBO), new PropertyInfo<string>("NumberReport"));
		private static PropertyInfo<int?> ViewCountProperty = RegisterProperty<int?>(typeof(ForumNewsBO), new PropertyInfo<int?>("ViewCount"));
        private static PropertyInfo<string> CategoryNameProperty = RegisterProperty<string>(typeof(ForumNewsBO), new PropertyInfo<string>("CategoryName"));
		[System.ComponentModel.DataObjectField(true, true)]
		public int ForumNewID
		{
			get { return GetProperty<int>(ForumNewIDProperty); }
		}
	 
		public string UserName
		{
			get { return GetProperty<string>(UserNameProperty); }
			set { SetProperty<string>(UserNameProperty, value); }
		}

        public int? SubTopicForumID
		{
            get { return GetProperty<int?>(SubTopicForumIDProperty); }
            set { SetProperty<int?>(SubTopicForumIDProperty, value); }
		}
	 
		public string Title
		{
			get { return GetProperty<string>(TitleProperty); }
			set { SetProperty<string>(TitleProperty, value); }
		}
	 
		public string ForumNewsContent
		{
			get { return GetProperty<string>(ForumNewsContentProperty); }
			set { SetProperty<string>(ForumNewsContentProperty, value); }
		}
	 
		public string UrlImage
		{
			get { return GetProperty<string>(UrlImageProperty); }
			set { SetProperty<string>(UrlImageProperty, value); }
		}
	 
		public DateTime? CreatedDate
		{
			get { return GetProperty<DateTime?>(CreatedDateProperty); }
			set { SetProperty<DateTime?>(CreatedDateProperty, value); }
		}
	 
		public DateTime? UpdatedDate
		{
			get { return GetProperty<DateTime?>(UpdatedDateProperty); }
			set { SetProperty<DateTime?>(UpdatedDateProperty, value); }
		}
	 
		public bool? IsViolation
		{
			get { return GetProperty<bool?>(IsViolationProperty); }
			set { SetProperty<bool?>(IsViolationProperty, value); }
		}

        public string UserNameReport
        {
            get { return GetProperty<string>(UserNameReportProperty); }
            set { SetProperty<string>(UserNameReportProperty, value); }
        }

        public string NumberReport
        {
            get { return GetProperty<string>(NumberReportProperty); }
            set { SetProperty<string>(NumberReportProperty, value); }
        }
	 
		public int? ViewCount
		{
			get { return GetProperty<int?>(ViewCountProperty); }
			set { SetProperty<int?>(ViewCountProperty, value); }
		}
        public string CategoryName
        {
            get { return GetProperty<string>(CategoryNameProperty); }
            set { SetProperty<string>(CategoryNameProperty, value); }
        }
		#endregion //Business Properties and Methods

		#region Validation Rules
		protected override void AddBusinessRules()
		{
			//
			// UserName
			//
			//ValidationRules.AddRule(CommonRules.StringRequired, UserNameProperty);
			//ValidationRules.AddRule(CommonRules.StringMaxLength, new CommonRules.MaxLengthRuleArgs(UserNameProperty, 50));
			//
			// Title
			//
			//ValidationRules.AddRule(CommonRules.StringRequired, TitleProperty);
			//ValidationRules.AddRule(CommonRules.StringMaxLength, new CommonRules.MaxLengthRuleArgs(TitleProperty, 150));
			//
			// ForumNewsContent
			//
			//ValidationRules.AddRule(CommonRules.StringRequired, ForumNewsContentProperty);
			//
			// UrlImage
			//
			//ValidationRules.AddRule(CommonRules.StringMaxLength, new CommonRules.MaxLengthRuleArgs(UrlImageProperty, 100));
			//
			// UserNameReport
			//
			//ValidationRules.AddRule(CommonRules.StringMaxLength, new CommonRules.MaxLengthRuleArgs(UserNameReportProperty, 50));

			AddCustomRules();
		}

		partial void AddCustomRules();
		#endregion //Validation Rules

		#region Factory Methods
		public static ForumNewsBO NewForumNewsBO()
		{
			return DataPortal.Create<ForumNewsBO>();
		}

		public static ForumNewsBO GetForumNewsBO(int forumNewID)
		{
			return DataPortal.Fetch<ForumNewsBO>(new SingleCriteria<ForumNewsBO, int>(forumNewID));
		}

		public static void DeleteForumNewsBO(int forumNewID)
		{
			DataPortal.Delete(new SingleCriteria<ForumNewsBO, int>(forumNewID));
		}

		private ForumNewsBO()
		{ /* require use of factory method */ }

		public ForumNewsBO CopyForumNewsBO()
		{
			ForumNewsBO objectCopy = this.Clone();
			objectCopy.MarkNew();
			return objectCopy;
		}
		#endregion //Factory Methods


		#region Data Access

		#region Data Access - Create
		[RunLocal]
		protected override void DataPortal_Create()
		{
			bool cancel = false;
			OnCreating(ref cancel);
			if (cancel) return;

			ValidationRules.CheckRules();

			//OnCreated();
		}

		partial void OnCreating(ref bool cancel);
		//partial void OnCreated();
		#endregion //Data Access - Create

		#region Data Access - Fetch
		private void DataPortal_Fetch(SingleCriteria<ForumNewsBO, int> criteria)
		{
			bool cancel = false;
			OnFetching(criteria, ref cancel);
			if (cancel) return;

			using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
						.GetManager(Database.HousingInfoConnection))
			{
				var data = mgr.DataContext.ForumNews
						.Single(r => r.ForumNewID == criteria.Value);
                var datasubcategory = mgr.DataContext.SubTopicForums
                        .Single(r => r.SubTopicForumID == data.SubTopicForumID);
				OnMemberLoading(data);
				LoadProperty<int>(ForumNewIDProperty, data.ForumNewID);
				LoadProperty<string>(UserNameProperty, data.UserName);
				LoadProperty<int?>(SubTopicForumIDProperty, data.SubTopicForumID);
				LoadProperty<string>(TitleProperty, data.Title);
				LoadProperty<string>(ForumNewsContentProperty, data.ForumNewsContent);
				LoadProperty<string>(UrlImageProperty, data.UrlImage);
				LoadProperty<DateTime?>(CreatedDateProperty, data.CreatedDate);
				LoadProperty<DateTime?>(UpdatedDateProperty, data.UpdatedDate);
				LoadProperty<bool?>(IsViolationProperty, data.IsViolation);
                LoadProperty<string>(UserNameReportProperty, data.UserNameReport);
                LoadProperty<string>(NumberReportProperty, data.NumberReport);
				LoadProperty<int?>(ViewCountProperty, data.ViewCount);
                LoadProperty<string>(CategoryNameProperty, datasubcategory.SubTopicForumName); 
				//OnMemberLoaded();

				ValidationRules.CheckRules();
			}//using

			//OnFetched();
		}

		partial void OnFetching(SingleCriteria<ForumNewsBO, int> criteria, ref bool cancel);
		//partial void OnFetched();
		partial void OnMemberLoading(HousingInfo.DataLinQ.ForumNew data);
		//partial void OnMemberLoaded();
		#endregion //Data Access - Fetch

		#region Data Access - Insert
		protected override void DataPortal_Insert()
		{
			bool cancel = false;
			OnInserting(ref cancel);
			if (cancel) return;

			using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
						.GetManager(Database.HousingInfoConnection))
			{
				var data = new HousingInfo.DataLinQ.ForumNew();

				OnMemberReading(data);

				data.UserName = ReadProperty<string>(UserNameProperty);
				data.SubTopicForumID = ReadProperty<int?>(SubTopicForumIDProperty).Value;
				data.Title = ReadProperty<string>(TitleProperty);
				data.ForumNewsContent = ReadProperty<string>(ForumNewsContentProperty);
				data.UrlImage = ReadProperty<string>(UrlImageProperty);
				data.CreatedDate = ReadProperty<DateTime?>(CreatedDateProperty).Value;
				data.UpdatedDate = ReadProperty<DateTime?>(UpdatedDateProperty);
				data.IsViolation = ReadProperty<bool?>(IsViolationProperty);
                data.UserNameReport = ReadProperty<string>(UserNameReportProperty);
                data.NumberReport = ReadProperty<string>(NumberReportProperty);
				data.ViewCount = ReadProperty<int?>(ViewCountProperty).Value;
				AssignParentKey(data);
				//OnMemberRead();

				mgr.DataContext.ForumNews.InsertOnSubmit(data);
				mgr.DataContext.SubmitChanges();

				LoadProperty<int>(ForumNewIDProperty, data.ForumNewID);
			}//using

			//OnInserted();
		}

		partial void OnInserting(ref bool cancel);
		//partial void OnInserted();
		partial void OnMemberReading(HousingInfo.DataLinQ.ForumNew data);
		//partial void OnMemberRead();
		#endregion //Data Access - Insert

		#region Data Access - Update
		protected override void DataPortal_Update()
		{
			bool cancel = false;
			OnUpdating(ref cancel);
			if (cancel) return;

			using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
						.GetManager(Database.HousingInfoConnection))
			{
                //var data = new HousingInfo.DataLinQ.ForumNew() 
                //{
                //    ForumNewID = ReadProperty<int>(ForumNewIDProperty)
                //};
                //mgr.DataContext.ForumNews.Attach(data);
                var data=mgr.DataContext.ForumNews.Single(c=>c.ForumNewID== ReadProperty<int>(ForumNewIDProperty));
				OnMemberReading(data);
				if (IsSelfDirty)
				{
					data.UserName = ReadProperty<string>(UserNameProperty);
					data.SubTopicForumID = ReadProperty<int?>(SubTopicForumIDProperty).Value;
					data.Title = ReadProperty<string>(TitleProperty);
					data.ForumNewsContent = ReadProperty<string>(ForumNewsContentProperty);
					data.UrlImage = ReadProperty<string>(UrlImageProperty);
					data.CreatedDate = ReadProperty<DateTime?>(CreatedDateProperty).Value;
					data.UpdatedDate = ReadProperty<DateTime?>(UpdatedDateProperty);
					data.IsViolation = ReadProperty<bool?>(IsViolationProperty);
                    data.UserNameReport = ReadProperty<string>(UserNameReportProperty);
                    data.NumberReport = ReadProperty<string>(NumberReportProperty);
					data.ViewCount = ReadProperty<int?>(ViewCountProperty).Value;
				}
				//OnMemberRead();

				mgr.DataContext.SubmitChanges();
				if (IsSelfDirty)
				{
					LoadProperty<int>(ForumNewIDProperty, data.ForumNewID);
				}
			}//using

			//OnUpdated();
		}
		partial void OnUpdating(ref bool cancel);
		//partial void OnUpdated();
		#endregion //Data Access - Update


		#region Data Access - Delete
		protected override void DataPortal_DeleteSelf()
		{
			bool cancel = false;
			OnSelfDeleting(ref cancel);
			if (cancel) return;

			using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
						.GetManager(Database.HousingInfoConnection))
			{
				OnMemberSelfDeleting();

				var data = new HousingInfo.DataLinQ.ForumNew() 
				{
					ForumNewID = ReadProperty<int>(ForumNewIDProperty)
				};
				mgr.DataContext.ForumNews.Attach(data);

				mgr.DataContext.ForumNews.DeleteOnSubmit(data);
				OnMemberSelfDeleted();

				mgr.DataContext.SubmitChanges();
			}//using

			//OnSelfDeleted();
		}

		partial void OnSelfDeleting(ref bool cancel);
		//partial void OnSelfDeleted();
		partial void OnMemberSelfDeleting();
		partial void OnMemberSelfDeleted();

		private void DataPortal_Delete(SingleCriteria<ForumNewsBO, int> criteria)
		{
			bool cancel = false;
			OnDeleting(criteria, ref cancel);
			if (cancel) return;

			using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
						.GetManager(Database.HousingInfoConnection))
			{
				var data = mgr.DataContext.ForumNews
						.Single(r => r.ForumNewID == criteria.Value);

				OnMemberDeleting(data);

				mgr.DataContext.ForumNews.DeleteOnSubmit(data);
				OnMemberDeleted();

				mgr.DataContext.SubmitChanges();
			}//using

			OnDeleted();
		}

		partial void OnDeleting(SingleCriteria<ForumNewsBO, int> criteria, ref bool cancel);
		partial void OnDeleted();
		partial void OnMemberDeleting(HousingInfo.DataLinQ.ForumNew data);
		partial void OnMemberDeleted();
		#endregion //Data Access - Delete
		#endregion //Data Access

	}
}
